package com.devg.file.client.core;

import com.devg.file.client.core.async.AsyncUtil;
import com.devg.file.client.core.entity.FileClientConfig;
import com.devg.file.client.core.entity.FileResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.springframework.web.multipart.MultipartFile;

public class WebUploadMultipartFile extends WebServiceRequest<MultipartFile> {

  @Override
  public FileResponse invoke(MultipartFile file, FileClientConfig fileClientConfig, String requestType) {
    InputStream inputStream = null;
    try {
      inputStream = file.getInputStream();
    } catch (Exception ex) {
      FileResponse fileResponse = new FileResponse();
      fileResponse.setStatusCode(500);
      fileResponse.setMessage(ex.getMessage());
      return fileResponse;
    }
    String fileName = file.getOriginalFilename();
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("file", inputStream, ContentType.MULTIPART_FORM_DATA, fileName);
    fillMultipartEntityBuilder(builder, fileClientConfig);
    return invokeFile(builder, fileClientConfig, requestType);
  }

  @Override
  public FileResponse invoke(MultipartFile file, String requestType) {
    return invoke(file, WebServiceRequest.fileClientConfig, requestType);
  }

  @Override
  public List<FileResponse> invoke(List<MultipartFile> fileList, FileClientConfig fileClientConfig, String requestType) {
    List<FileResponse> list = new ArrayList<>();
    if (null != fileList && fileList.size() > 0) {
      AsyncUtil asyncUitl = AsyncUtil.newInstance();
      try {
        Class[] paramClass = new Class[]{MultipartFile.class, FileClientConfig.class, String.class};
        for (int i = 0; i < fileList.size(); i++) {
          MultipartFile multipartFile = fileList.get(i);
          Object[] paramObject = new Object[]{multipartFile, fileClientConfig, requestType};
          asyncUitl.addExecutePool("multipartFile" + i, this, "invoke", paramObject, paramClass);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        asyncUitl.shutdown();
      }
      for (int i = 0; i < fileList.size(); i++) {
        FileResponse fileResponse = asyncUitl.getResult("multipartFile" + i);
        list.add(fileResponse);
      }
    }
    return list;
  }

  @Override
  public List<FileResponse> invoke(List<MultipartFile> fileList, String requestType) {
    List<FileResponse> list = new ArrayList<>();
    if (null != fileList && fileList.size() > 0) {
      AsyncUtil asyncUitl = AsyncUtil.newInstance();
      try {
        Class[] paramClass = new Class[]{MultipartFile.class, String.class};
        for (int i = 0; i < fileList.size(); i++) {
          MultipartFile multipartFile = fileList.get(i);
          Object[] paramObject = new Object[]{multipartFile, requestType};
          asyncUitl.addExecutePool("multipartFile" + i, this, "invoke", paramObject, paramClass);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        asyncUitl.shutdown();
      }
      for (int i = 0; i < fileList.size(); i++) {
        FileResponse fileResponse = asyncUitl.getResult("multipartFile" + i);
        list.add(fileResponse);
      }
    }
    return list;
  }
}
